#!/bin/bash

# 检查是否提供了 commit message
if [ $# -eq 0 ]; then
    echo "Error: Please provide a commit message."
    echo "Usage: ./git_push_all.sh \"Your commit message\""
    exit 1
fi

COMMIT_MSG="$1"

# 进入 src 目录
cd ~/shangweiji/src || { echo "Failed to enter src/"; exit 1; }

# 定义要操作的仓库列表
REPOS=(
    "robot_app"
    "robot_common"
    "robot_map"
    "robot_msgs"
    "robot_auto_drill"
    "robot_common"
)

# 遍历所有仓库并执行 git 操作
for REPO in "${REPOS[@]}"; do
    echo "Checking $REPO..."
    if [ -d "$REPO" ]; then
        cd "$REPO" || { echo "Failed to enter $REPO"; exit 1; }

        # 检查是否有未提交的更改
        if [ -n "$(git status --porcelain)" ]; then
            echo "Changes detected. Committing and pushing..."
            git add . || { echo "git add failed in $REPO"; exit 1; }
            git commit -m "$COMMIT_MSG" || { echo "git commit failed in $REPO"; exit 1; }
            git push || { echo "git push failed in $REPO"; exit 1; }
            echo "Successfully pushed $REPO!"
        else
            echo "No changes in $REPO, skipping."
        fi

        cd ..  # 返回上级目录
        echo "----------------------------------"
    else
        echo "Warning: Directory $REPO not found!"
    fi
done

echo "All repositories processed!"

